package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.DictMapper;
import com.bwie.poi.ExcelUtil;
import com.bwie.pojo.Dict;
import com.bwie.pojo.Shop;
import com.bwie.service.ShopService;
import com.bwie.mapper.ShopMapper;
import com.bwie.state.ShopStatusEnum;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import com.bwie.vo.ShopDataVo;
import com.bwie.vo.ShopDayDataVo;
import com.bwie.vo.ShopPieDataVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileFilter;
import java.io.IOException;
import java.util.*;

/**
* @author 16836
* @description 针对表【t_shop】的数据库操作Service实现
* @createDate 2025-05-29 14:55:15
*/
@Service
@Transactional
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop>
    implements ShopService{
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    @Override
    public R getShopList(PageInfoVo pageInfoVo) {
        Page<Shop> shopPage = new Page<>(pageInfoVo.getPageNum(),pageInfoVo.getPageSize());
        Page<Shop> shopList = shopMapper.getShopList(shopPage,pageInfoVo.getKeyWord());
        return R.OK(shopList);
    }

    @Override
    public R getShopType(Integer typeId) {
        List<Dict> dicts = dictMapper.selectList(
                new QueryWrapper<Dict>().lambda().eq(Dict::getDictType, typeId)
        );
        return R.OK(dicts);
    }

    @Override
    public R addShop(Shop shop) {
        //解析
        String token = request.getHeader("token");
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        Integer id = body.get("id", Integer.class);
        shop.setUserId(id);
        shop.setCreateTime(new Date());
        shop.setShopStatus(ShopStatusEnum.SHOP_STATUS_DOWN.getCode());
        Shop shop1 = shopMapper.selectOne(
                new QueryWrapper<Shop>().lambda().eq(Shop::getShopName, shop.getShopName())
        );
        if(ObjectUtils.isNotEmpty(shop1)){
            return R.ERROR(500,"商品已存在");
        }
        shopMapper.insert(shop);
        return R.OK();
    }

    @Override
    public void exprotExcel() {
        List<Shop> shops = shopMapper.selectList(null);
        ExcelUtil excelUtil = new ExcelUtil(Shop.class);
        excelUtil.exportExcel(response,shops,"商品列表");
    }

    @Override
    public R uploadFile(MultipartFile file) {
        String s = FileNameUtil.extName(file.getOriginalFilename());
        try {
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), s, null);
            String url = "http://192.168.80.192:8888/"+storePath.getFullPath();
            return R.OK(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ERROR(500,"上传失败");
    }

    @Override
    public R updateShopStatus(Shop shop) {
        Shop shop1 = shopMapper.selectById(shop.getId());
        if(ObjectUtils.isEmpty(shop1)){
            return R.ERROR(500,"商品不存在");
        }
        shop1.setContent(shop.getContent());
        shop1.setShopStatus(shop.getShopStatus());
        shopMapper.updateById(shop1);
        return R.OK();
    }

    @Override
    public R getShopData() {
        ShopDataVo shopDataVo = shopMapper.getshopData();
        return R.OK(shopDataVo);
    }

    @Override
    public R getShopDayData() {
        List<ShopDayDataVo> shopDayDataVos = shopMapper.getShopDayData();

        ArrayList<String> strings = new ArrayList<>();
        ArrayList<Integer> integers = new ArrayList<>();
        ArrayList<ShopPieDataVo> shopPieDataVos = new ArrayList<>();
        for (ShopDayDataVo shopDayDataVo : shopDayDataVos) {
            strings.add(shopDayDataVo.getDayTime());
            integers.add(shopDayDataVo.getUpCount());
            ShopPieDataVo shopPieDataVo = new ShopPieDataVo();
            shopPieDataVo.setName(shopDayDataVo.getDayTime());
            shopPieDataVo.setValue(shopDayDataVo.getUpCount());
            shopPieDataVos.add(shopPieDataVo);
        }

        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("dayTime",strings);
        stringObjectHashMap.put("upCount",integers);
        stringObjectHashMap.put("pieData",shopPieDataVos);
        return R.OK(stringObjectHashMap);
    }
}




